using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Security;
using System.Data.Entity;
using DomainModel.Concrete;
using DomainModel.Entities;

namespace WebUI.Controllers
{

    public class CompanyController : Controller
    {
        SqlCompanyRepository _companyRepository;
        SqlCompanyTypeRepository _companyTypeRepository;
        #region PrivateFunctions
        private ActionResult HandleMyError(Exception ex, string actionName)
        {
            var modelErr = new HandleErrorInfo(ex, "CompanyController", actionName);
            return View("Error", modelErr);
        }
        #endregion
        public CompanyController() 
        { 
            _companyRepository = new SqlCompanyRepository();
            _companyTypeRepository = new SqlCompanyTypeRepository();
        }
        #region PublicActions
        public ActionResult Index()
        {
            try
            {
                return View(_companyRepository.GetAll());
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }

        }

        public ActionResult Details(int id)
        {
            try
            {
                return View(_companyRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "CreateAction");
            }
        }

        public ActionResult Create()
        {
            try
            {
                Company company = new Company();
                ViewBag.CompanyTypes = _companyTypeRepository.GetAll().OrderBy(ct => ct.Name).ToList();
                return View(company);              
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Create Company Get Action");
            }

        }

        [HttpPost]
        public ActionResult Create(Company company, FormCollection collection)
        {
            company.LastUpdate = DateTime.Now;
            company.UserName = Membership.GetUser().UserName;
            company.IsApproved = false;


            if (ModelState.IsValid)
            {
                try
                {                    
                    _companyRepository.Insert(company);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "Create Company Post Action");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Edit(int id)
        {
            ViewBag.CompanyTypes = _companyTypeRepository.GetAll().OrderBy(ct => ct.Name).ToList();
            return View(_companyRepository.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(Company company, int id, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    _companyRepository.Update(company);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "EditAction");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                return View(_companyRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Get Delete Action");
            }
            
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _companyRepository.Delete(_companyRepository.GetById(id));
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "DeleteAction");
            }
        }
        #endregion
    }
}
